package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.CommonConstantUtil.CommonStatus;
import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.DstMemberInfoDTO;
import com.financing.wap.dto.DstUserIntegralFlowDTO;
import com.financing.wap.service.IDstMemberInfoService;
import com.financing.wap.service.IDstUserIntegralFlowService;
import com.financing.wap.util.StringUtil;
import com.financing.wap.web.controller.BaseController;

@Controller("dstUserIntegralFlowController")
@RequestMapping("/api/dstUserIntegralFlow")
public class DstUserIntegralFlowController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IDstUserIntegralFlowService dstUserIntegralFlowService;
	
	@Autowired
	private IDstMemberInfoService dstMemberInfoService;
	/***
	 * 用户积分流水
	 * @param request
	 * @param dstUserIntegralFlow
	 * @param pager
	 * @return
	 */
	@RequestMapping("/listDstUserIntegralFlow")
	@ResponseBody
	public ResultMap list(HttpServletRequest request,DstUserIntegralFlowDTO dstUserIntegralFlow,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query dstUserIntegralFlow list begin .......");
			
			String openid = request.getParameter("openid");
			if(StringUtil.isNullOrEmptyStr(openid)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DstMemberInfoDTO dstMemberInfo=new DstMemberInfoDTO();
			dstMemberInfo.setUserOpenid(openid);
			DstMemberInfoDTO dto = dstMemberInfoService.queryListGetFirst(dstMemberInfo);
			result.setAttribute("dto",dto);
			
			dstUserIntegralFlow.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
			dstUserIntegralFlow.setUserOpenid(openid);
			dstUserIntegralFlow.setBegin(pager.getStartIndex());
			dstUserIntegralFlow.setPageSize(pager.getPageSize());
			dstUserIntegralFlow.setIntegralStatus("2");
			
			int count = dstUserIntegralFlowService.queryListCount(dstUserIntegralFlow);
			
			pager.setTotalCount(count);
			
			List<DstUserIntegralFlowDTO> dstUserIntegralFlowList = null;
			
			if(count > 0){
				dstUserIntegralFlowList = dstUserIntegralFlowService.queryList(dstUserIntegralFlow);
			}else{
				dstUserIntegralFlowList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", dstUserIntegralFlowList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query dstUserIntegralFlow list end .......");
		}catch(Exception e){
			log.error("query dstUserIntegralFlow error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
